/*
 * SignExample.c
 *
 *  Created on: 2013年5月15日
 *      Author: drowning
 */
#include <signal.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>


void handler(int sig_num) {
	printf("child process has exited : %d \n" , sig_num);
}

void sign() {

	pid_t pid , pr;
	sigset_t block;
	struct sigaction action , oldaction;

	sigemptyset(&action.sa_mask);
	//接受到信号通知后,调用handler函数
	action.sa_handler = handler;
	//添加SIGQUIT阻塞信号
	sigaddset(&block, SIGQUIT);
	action.sa_mask = block;
	action.sa_flags = 0;

	//注册信号SIGCHLD(捕获子进程退出)
	sigaction(SIGCHLD , &action , &oldaction);


	//模拟子进程退出
	pid = fork();
	if(pid == 0) {
		printf ("come to child process and sleep 3s\n" );
		sleep(3);
		exit(0);
	}
	//wait|waitpid函数等待子进程退出(子进程退出后会变成僵尸进程,wait|waitpid回收僵尸进程)
	//wait阻塞,waitpid可以选择 阻塞|飞阻塞
	int status;
	pr = wait(&status);
	if(WIFEXITED(status)) {
		printf("the child process exited normally %d \n" , pr);
		printf("return code is : %d \n" , WEXITSTATUS(status));
	}else {
		printf("the child proccess exited abnormally  %d \n" , pr);
	}
}

